package com.ikdong.toodle.util;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.Log;
import com.ikdong.toodle.dao.GoalDAO;
import com.ikdong.toodle.exception.TDException;
import com.ikdong.toodle.model.Goal;

public class GoalManager {
	private GoalDAO goalDao;
	private String userId;
	private String key;
	private TaskManager tm;
	
	public GoalManager(Context context){
		goalDao = new GoalDAO(context);
		tm = new TaskManager(context);
		userId = new AccountManager(context).getAccount().getUserid();
		key = new AuthManager(context).getKey();
	}
	
	private void syncNew() throws Exception{
		List<Goal> goals = goalDao.get(userId,Constant.STATUS_SYNC_ADD);
		for(Goal lgoal : goals){
			try {
				Goal sgoal = ToodleHttpClient.addGoal(key,lgoal);
				if(sgoal!=null){
					goalDao.replace(lgoal.getId().toString(), sgoal);
					tm.updateGoalChange(lgoal.getId(), sgoal.getId());
				}
			} catch (TDException e) {
				throw e;
			}
		}
	}
	
	private void syncDelete() throws Exception{
		List<Goal> goals = goalDao.get(userId,Constant.STATUS_SYNC_DELETE);
		for(Goal lgoal : goals){
			try {
				ToodleHttpClient.deleteGoal(key,lgoal.getId());
				goalDao.delete(lgoal.getId());
			} catch (TDException e) {
				Log.e(Constant.TAG, "syncGoalDelete()", e);
				goalDao.delete(lgoal.getId());
			}
		}
	}
	
	private void syncEdit() throws Exception{
		List<Goal> goals = getOnline();
		List<Long> ids = new ArrayList<Long>();
		for(Goal sgoal : goals){
			Long id = sgoal.getId();
			ids.add(id);
			Goal lgoal = goalDao.get(id);
			if(lgoal==null){
				goalDao.add(sgoal.getId(), sgoal.getName(), sgoal.getLevel(),sgoal.getArchived(),sgoal.getContributes(),sgoal.getNote(), Constant.STATUS_SYNC_DONE, userId);
			}else if(lgoal.getStatus()==Constant.STATUS_SYNC_MODIFY){
				Goal goal = ToodleHttpClient.editGoal(key, lgoal);
				goal.setStatus(Constant.STATUS_SYNC_DONE);
				goalDao.update(goal);
			}else if(!lgoal.toString().equals(sgoal.toString())){
				goalDao.update(sgoal);
			}
		}
		
		goalDao.deleteWithoutIds(ids);
	}
	
	public void sync(boolean isSyncUpdate)throws Exception{
		syncNew();
		syncDelete();
		if(isSyncUpdate) 
			syncEdit();
	}
	
	public List<Goal> getOnline(){
		List<Goal> goals = new ArrayList<Goal>();
		goals.addAll(ToodleHttpClient.getGoal(key));
		return goals;
	}
	
	public Goal getGoal(long id){
		return goalDao.get(id);
	}
	
	public Goal getGoal(String name){
		return goalDao.get(name);
	}
	
	public List<Goal> getGoal(){
		return goalDao.get(userId,Constant.STATUS_SYNC_ADD,Constant.STATUS_SYNC_DONE,Constant.STATUS_SYNC_MODIFY);
	}
	
	public String[] getGoalNames(){
		List<Goal> goals = getGoal();
		List<String> names = new ArrayList<String>();
		names.add("All");
		for(Goal goal : goals){
			names.add(goal.getName());
		}
		names.add("No Goal");
		return names.toArray(new String[names.size()]);
	}


}
